<template>
    <NavBar title="注册" left-text="返回" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit" class="form">
        <van-cell-group inset>
            <van-field v-model="username" name="手机号" label="手机号" placeholder="手机号"
                :rules="[{ required: true, message: '请填写手机号' }]" />
            <van-field v-model="sms" center clearable name="短信验证码" label="短信验证码" placeholder="请输入短信验证码"
                :rules="[{ required: true, message: '请填写验证码' }]">
                <template #button>
                    <van-button @click="yanZheng" size="small" type="primary">发送验证码</van-button>
                </template>
            </van-field>
            <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]" />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">
                提交
            </van-button>
        </div>
    </van-form>

</template>

<script lang="ts" setup>

import { showSuccessToast, showFailToast } from 'vant';

import { NavBar } from "vant";

import { useRouter } from 'vue-router'

import { getUniteLoginUrl, zhuCe } from "../api/index";

import { ref } from 'vue'
import type { Ref } from 'vue'


const router = useRouter();

const onClickLeft = () => router.push('/login');

const username: Ref<string> = ref('')

const sms: Ref<string> = ref('')

const password: Ref<string> = ref('')

const smscode = ref()


const onSubmit = async () => {
    if (sms.value == smscode.value) {
        const result = await zhuCe({
            tel: username.value,
            password: password.value
        })
        console.log(result);
        router.push('/login')
        showSuccessToast(result.data.message)
    } else {
        showFailToast('验证码错误');
    }
}
const yanZheng = async () => {
    const result = await getUniteLoginUrl(username.value)
    console.log(result);
    smscode.value = result.data.data
    console.log(smscode.value);

}

</script>

<style scoped>
.form {
    margin-top: 20px;
}
</style>